<?php
namespace Bshop\Controller;
class ReuseController extends CkadminController {
    /**
     * 活动选择标签
     */
    public function setlabel(){
    	$id = $_GET['id'];
    	if(!empty($id)){
    		$idarray = explode(",",$id);
    		foreach ($idarray as $i=>$va){
    			if(!empty($va)){
    				$idarraylist[] = $va;
    			}
    		}
    		$this->assign('idarraylist',$idarraylist);
    	}
    	$db = M('label');
    	$list = $db->where("close =1")->order("is_hot desc,sort desc,id desc")->select();
    	$this->assign('list',$list);
    	$this->display('Reuse/setlabel');
    }
    /**
     * 
     */
    public function look(){
    	$aid = (int)I('get.id');
    	if($aid){
    		$res = M('activity')->where("id='$aid'")->find();
    		$this->assign('va',$res);
    	}
     	$this->display('Reuse/look');
    }
    /**
     * 根据商圈的id来获取商圈的名字
     */
    public function circlename($id){
    	$db = M('circle');
    	$name = $db->where("id='$id'")->getField('name');
    	return $name;
    }
    /**
     * 根据id 来获取城市名字
     */
    public function cityname($citysid=null,$cityid=null,$areaid=null){
    	$db = M('citys');
    	if(!empty($citysid)){
    		$provincename = $db->where("id ='$citysid'")->getField('name');
    	}
    	if(!empty($cityid)){
    		$cityname = $db->where("id ='$cityid'")->getField('name');
    	}
    	if(!empty($areaid)){
    		$areaname = $db->where("id ='$areaid'")->getField('name');
    	}
    	$name = $provincename.$cityname.$areaname;
    	return $name;
    }
    /**
     * 选择商圈
     */
    public function selectcircle(){
    	if(IS_AJAX){
    		$citysid = (int)I('post.citysid');
    		$leve = M('citys')->where("id='$citysid'")->getField('leve');
    		if($leve == 1){
    			$where['provinceid'] = $citysid;
    		}elseif ($leve == 2){
    			$where['cityid'] = $citysid;
    		}else{
    			$where['areaid'] = $citysid;
    		}
    		$list = M('circle')->where($where)->field("id,name")->select();
    		echo jsonencode(200, $list);
    	}else{
    		echo jsonencode(300, "传输错误");
    	}
    }
    /**
     * 选择城市
     */
    public function selectcity(){
    	if(IS_AJAX){
    		$id = (int)I('post.id');
    		if(!empty($id)){
    			$list = M('region')->where("parent_id ='$id' and status='0'")->field("id,name")->select();
    			echo jsonencode(200, $list);
    		}else{
    			echo jsonencode(200, "");
    		}
    	}else{
    		echo jsonencode(300, "传输错误");
    	}
    }
    /**
     * 查看活动库存
     */
    public function repertory(){
    	$aid = (int)I('get.id');
    	if($aid){
    		$shop_id = M('activity')->where("id='{$aid}'")->field("activity_types,is_collect_information,information_title")->find();
    		$this->assign('shop_id',$shop_id);
    		$db1 = M('activity_ticket as at');
    		$list = $db1->join("".C('DB_PREFIX')."activity_field as af on af.id = at.field_id")->where("at.aid='{$aid}'")->field("at.id,at.number,at.true_price,af.activity_start_date,af.activity_start_time,af.activity_end_time")->select();
    		if(!empty($list)){
    			$db2 = M('order');
    			foreach ($list as $i=>$va){
    				//已售
    				$list[$i]['yi_shou'] = $db2->where("ticket_id='{$va['id']}' and pay_status ='1'")->sum('number');
    				if(empty($list[$i]['yi_shou'])){
    					$list[$i]['yi_shou'] = 0;
    				}
    			}
    		}
    		$this->assign('list',$list);
    	}
    	$this->display();
    }
    /**
     * 活动参与信息
     */
    public function activity_msg(){
    	$aid = (int)I('get.aid');
		$db = M('activity_msg as am');
		$shop_id = M('activity')->where("id='{$aid}'")->field("id,activity_types,is_collect_information,information_title")->find();
		$this->assign('shop_id',$shop_id);
		$msg = json_decode($shop_id['information_title'],true);
		$msglist = $msg['information_title']?$msg['information_title']:array();
		if(!empty($msg['order_title'])){
			foreach ($msg['order_title'] as $k=>$vv){
				if(!empty($vv)){
					array_push($msglist, $vv);
				}
			}
		}
		$this->assign('msg',$msglist);
		$list = $db->join("left join ".C('DB_PREFIX')."order as o on am.order_no = o.order_no left join ".C('DB_PREFIX')."activity_ticket as at on at.id = o.ticket_id left join ".C('DB_PREFIX')."user as u on am.uid = u.id left join ".C('DB_PREFIX')."activity_field as af on af.id = o.field_id")->where("am.aid='{$aid}' and o.pay_status='1'")->field("am.uid,am.order_no,am.content,o.ticket_date,at.ticket_name,u.username,af.activity_start_time,af.is_order")->order("am.id asc")->select();
		foreach ($list as $i=>$va){
			$list[$i]['activity_start_time'] = \Org\Web\activity::ticket_time($va['activity_start_time'],$va['is_order']);
			$list[$i]['content'] = json_decode($va['content'],true);
			$list[$i]['content_title'] = array();
			foreach ($list[$i]['content'] as $l=>$vl){
				$list[$i]['content_title'][] = $vl['title'];
			}
		}
		$this->assign('list',$list);
		$this->display();
    }
    /**
     * 导出活动参与信息
     */
    public function export_activity_msg(){
    	$aid = I('aid');
    	$db = M('activity_msg as am');
    	$list = $db->join("left join ".C('DB_PREFIX')."order as o on am.order_no = o.order_no left join ".C('DB_PREFIX')."activity_ticket as at on at.id = o.ticket_id left join ".C('DB_PREFIX')."user as u on am.uid = u.id left join ".C('DB_PREFIX')."activity_field as af on af.id = o.field_id")->where("am.aid='{$aid}' and o.pay_status='1'")->field("am.uid,am.order_no,am.content,o.ticket_date,at.ticket_name,u.username,af.activity_start_time,af.is_order")->order("am.id asc")->select();
    	foreach ($list as $i=>$va){
    		$list[$i]['activity_start_time'] = \Org\Web\activity::ticket_time($va['activity_start_time'],$va['is_order']);
    		$list[$i]['content'] = json_decode($va['content'],true);
    		$list[$i]['html'] = "";
    		foreach ($list[$i]['content'] as $l =>$vl){
    			if($vl['title']!="附件"){
    				$list[$i]['html'] .= $vl['title'].":".$vl['content']."     ";
    			}
    		}
    	}
    	$fileName = "参与者信息";
    	$this->getExcel($fileName,$list);
    }
    private  function getExcel($fileName,$data){
    	//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
    	import("Org.Util.PHPExcel");
    	import("Org.Util.PHPExcel.Writer.Excel5");
    	import("Org.Util.PHPExcel.IOFactory.php");
    	import("Org.Util.PHPExcel.style.Alignment.php");
    	$date = date("Y_m_d",time());
    	$fileName .= "_{$date}.xls";
    	//创建PHPExcel对象，注意，不能少了\
    	$objPHPExcel = new \PHPExcel();
    	$objProps = $objPHPExcel->getProperties();
    	$objPHPExcel->setActiveSheetIndex(0)
    	->setCellValue('A1',"购买者手机号")
    	->setCellValue('B1',"订单号")
    	->setCellValue('C1',"场次及票名")
    	->setCellValue('D1',"购买者信息");
    	foreach($data as $k=>$v){
    		$num = $k+2;
    		$objPHPExcel->setActiveSheetIndex(0)
    		//Excel的第A列，uid是你查出数组的键值，下面以此类推
    		->setCellValue('A'.$num, $v["username"])
    		->setCellValue('B'.$num, $v["order_no"])
    		->setCellValue('C'.$num, $v["ticket_date"]." ".$v['activity_start_time']." ".$v['ticket_name'])
    		->setCellValue('D'.$num, $v["html"]);
    	}
    	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(24);
    	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(24);
    	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(24);
    	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(180);
    	$objPHPExcel->getActiveSheet()->getStyle('A1:N'.($num))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    	$fileName = iconv("utf-8", "gb2312", $fileName);
    		
    	//重命名表
    	//$objPHPExcel->getActiveSheet()->setTitle('test');
    	//设置活动单指数到第一个表,所以Excel打开这是第一个表
    	$objPHPExcel->setActiveSheetIndex(0);
    	ob_end_clean();//清除缓冲区,避免乱码
    	header('Content-Type: application/vnd.ms-excel');
    	header("Content-Disposition: attachment;filename=\"$fileName\"");
    	header('Cache-Control: max-age=0');
    
    	$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    	$objWriter->save('php://output'); //文件通过浏览器下载
    	exit;
    }
}